# libcexceptions

## Summary

Tiny C library, which allows you to catching exceptions.

**IMPORTANT: _Returning a value (exiting a function) inside the context of catching an exception will cause your program to work incorrectly_**

## Installation
* `$ make cexceptions` or `$ make all` - builds distr
```sh
$ tree distr
build
├── include
│   └── cexceptions.h
└── lib
    └── libcexceptions.so
```
* make install - builds distr and copies it's content (`build/*`) to `prefix_path` (by default `/usr`)

## Usage example:  

```c
#include <stdio.h>
#include <cexceptions/cexceptions.h>

void nested_function(){
  throw(THROWABLE_INVALID_ARGUMENT, "test", -1, NULL);  //Throws an Exception with type=2
}

int main(int argc, char ** args){
  try(){                                                // Pushes new TryCatchContext CONTEXT1 into defaultTryCatchContextStack
    try(){                                              // Pushes new TryCatchContext CONTEXT2 into defaultTryCatchContextStack
      nested_function();
    }catch(THROWABLE_RUNTIME_ERROR){                    // Cathces exception with type=EXCEPTION thrown in CONTEXT2 of defaultTryCatchContextStack
    }catch(){                                           // Catches Exceptions with any type differs catched before
      propagate();                                      // Propagates unhandled Exception to CONTEXT1 of defaultTryCatchContextStack
    }etry()                                             // Finalizes CONTEXT2
  }catch(){                                             // Catches Exceptions with any type thrown in CONTEXT1 (or propagated from nested contexts)
    fprintf(stderr, "%T\n", thrown());                  // Printing catched Exception
  }etry()                                               // Finalizes CONTEXT1
}
```